#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/10/12 17:42
# @Author  : AshJo
# @Desc    : 线程锁
# @Filename: thread_lock.py
# @Version : v1.0
"""

import time
from threading import Thread

loop = int(1E7)


def _add(loop: int = 1):
    global numbers
    for _ in range(loop):
        numbers.append(0)


def _sub(loop: int = 1):
    global numbers
    for _ in range(loop):
        while not numbers:
            time.sleep(1E-8)
        numbers.pop()


if __name__ == '__main__':
    numbers = [0]
    ta = Thread(target=_add, args=(loop,))
    ts = Thread(target=_sub, args=(loop,))
    ta.start()
    ts.start()
    ta.join()
    ts.join()
